from flask import Blueprint,jsonify,request,abort
from flask_restful import Api,Resource
from app.models import *
# main = Blueprint("main", __name__)
employment = Api()
class Employment1(Resource):
    def get(self):
        employment = Employment.query.all()
        return jsonify({"employment": list(map(convert_list4, employment))})
    def post(self):
        employment_add_one = Employment(
            request.json.get('name'),
            request.json.get('phone'),
            request.json.get('wechat'),
            request.json.get('salary'),
            request.json.get('company'),
            request.json.get('place'),
            request.json.get('gradu_time'),
            request.json.get('work_time')

        )
        db.session.add(employment_add_one)
        db.session.commit()
        return jsonify({'employment_add_one': convert_list4(employment_add_one)})
employment.add_resource(Employment1,'/employment1/')

class Employment2(Resource):
    def get(self,uid):
        employment_one = Employment.query.filter_by(id=uid).first()
        if employment_one is None:
            abort(404)
        return jsonify({'employment_one': convert_list4(employment_one)})
    def put(self,uid):
        employment_update = Employment.query.get(uid)
        employment_update.name = request.json.get('name')
        employment_update.phone = request.json.get('phone')
        employment_update.wechat = request.json.get('wechat')
        employment_update.company = request.json.get('company')
        employment_update.place = request.json.get('place')
        employment_update.gradu_time = request.json.get('gradu_time')
        employment_update.work_time = request.json.get('work_time')

        db.session.add(employment_update)
        db.session.commit()
        return jsonify({'employment_update': convert_list4(employment_update)})
    def delete(self, uid):
        tasks = Employment.query.filter_by(id=uid, mark=1).first()
        if tasks:
            tasks.mark = 0
            return "数据已经删除"
        return jsonify({"notfound": "无法删除"})
employment.add_resource(Employment2, "/employment2/<int:uid>/")